﻿using System;
using System.Windows.Forms;
using Interfaces.Update_Interfaces;
using Interfaces.Information_Interfaces;
using Repository;
using Model.Domain_Model;
using Presenters.Inherited_Presenters;

namespace Presenters.Update_Presenters
{
    public class ClientUpdatePresenter:OperationStatus
    {
        IClientUpdateView view = null;
        IDb database = Repo.GetDatabaseInstance();
        public Client client = null;

        public ClientUpdatePresenter(IClientUpdateView view)
        {
            this.view = view;
        }

        /// <summary>
        /// Przesyła dane do widoku
        /// </summary>
        public void SendDataToView()
        {
            view.nameClient = client.nameClient;
            view.surnameClient = client.surnameClient;
            view.phoneNumber = client.phoneNumber;
            view.isProtected = client.isProtected;
            view.inventedName = client.inventedName;
            view.email = client.email;
            view.nameCorporation = client.nameCorporation;
            view.descriptionClient = client.descriptionClient;
        }

        /// <summary>
        /// Aktualizuje obiekt typu Client w bazie danych
        /// </summary>
        public void Update()
        {
            nameOperation = "Aktualizacja klienta";
            try
            {
                database.Clients.UpdateClient(new Client { idClient = client.idClient, nameClient = view.nameClient, surnameClient = view.surnameClient, dateInsert = client.dateInsert, descriptionClient = view.descriptionClient, email = view.email, inventedName = view.inventedName, isProtected = view.isProtected, nameCorporation = view.nameCorporation, phoneNumber = view.phoneNumber });
                view.ShowSuccess();
                view.Close();
            }
            catch (Exception ex)
            {
                exception = ex.Message;
                view.ShowError();
            }
        }
    }
}
